//
//  WBAuthorControllerViewController.m
//  TransWB
//
//  Created by 孝飞王 on 2017/1/10.
//  Copyright © 2017年 孝飞王. All rights reserved.
//

#import "WBAuthorControllerViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@interface WBAuthorControllerViewController ()<UIWebViewDelegate>{
    UIWebView *webView;
}

@end

@implementation WBAuthorControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"登录新浪微博";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:[UIButton cz_titleAndImage:@"navigationbar_back_withtext" title:@"返回" taget:self action:@selector(backBtn)]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:[UIButton cz_titleAndImage:nil title:@"自动填充" taget:self action:@selector(aotuFill)]];
    
    [self setUpWebView];
    
}

- (void)setUpWebView{
    
    webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    webView.backgroundColor = [UIColor whiteColor];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",App_Key,back_Url]]];
    [webView loadRequest:request];
    webView.delegate = self;
    [self.view addSubview:webView];
    
}
- (void)backBtn{
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)aotuFill{
    
    NSString *js = @"document.getElementById('userId').value = 18336070775;";
    [webView stringByEvaluatingJavaScriptFromString:js];
    js =                   @"document.getElementById('passwd').value = WANGFEI; ";
    [webView stringByEvaluatingJavaScriptFromString:js];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    [SVProgressHUD show];
    //code=7bb86ef9029fdd0e4bbb2b001ce1a790
    //code=7bb86ef9029fdd0e4bbb2b001ce1a790&from=844b&vit=fps
    NSLog(@"加载 ----%@",request.URL.absoluteString);
    NSLog(@"加载query -- %@",request.URL.query);
    //1. 如果 含有 baidu的话就不加载 否则加载
    
    if(![request.URL.absoluteString hasPrefix:back_Url]){
        return YES;
    }
    
    //2. 查找 code
    
    if (![request.URL.query hasPrefix:@"code"]) {
        [self backBtn];
        [SVProgressHUD dismiss];
        return NO;
    }
    
    //3 看是否有code
     NSString *code =  [request.URL.query substringFromIndex:5];
    NSLog(@"code = %@",code);
    //换取网络请求
    
    [[WBNetworkManager shareManager] loadAccessToken:code completion:^(BOOL isSuccess) {
        
        if(! isSuccess){
            //
//        [SVProgressHUD text];
            [self backBtn];
            [SVProgressHUD dismiss];
        }else{
            
            [[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccess object:nil];
            [self backBtn];
            [SVProgressHUD dismiss];
        }
        
        
    }];
    
   
    return NO;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}

@end
